<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;

class UserController extends Controller
{
    //
    public function index(User $userModel){
        $res = $userModel::get();
        return view('admin.user.index',['res'=>$res]);
    }

    public function create(Request $request){
        return view('admin.user.create');
    }

    public function store(Request $request){
        $data = $request->except('_token');
        $res = User::create($data);
        if($res){
            return redirect('admin/user/index');
        }else{
            return redirect()->back();
        }
    }

    public function edit($id,User $userModel){
        $title = '编辑用户';
        $userInfo = $userModel::find($id);
        if(empty($userInfo)){
            return redirect()->back();
        }
        $list = compact('title','userInfo');
        return view('admin.user.edit',$list);
    }

    public function update($id,Request $request){
        $data = $request->except('_token');
        User::where('id',$id)->update($data);
        return redirect('admin/user/index');
/*        $user = User::find($id);

        $user->name = $request->name;
        $user->password = $request->password;

        $res = $user->save();
        dd($res);*/
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy($id){
        User::destroy($id);
        return redirect()->back();
    }

    /**
     * 回复文章
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restore($id){
        User::where('id',$id)->restore();
        return redirect()->back();
    }

    public function delete($id){
        User::where('id',$id)->delete();
        return redirect()->back();
    }

    public function forceDelete($id){
        User::where('id',$id)->forceDelete();
        return redirect()->back();
    }
}
